# -*- coding: utf-8 -*-,
# @File : 3.多任务操作.py,
# @Author : lidong,
# @IDEA: PyCharm
# @Time :  2022/5/11 15:45

import time
import asyncio


async def get_request(url):
    print('正在请求的url:', url)
    # time.sleep(3) 不支持异步，使用该延迟会阻断异步
    await asyncio.sleep(2)
    print('请求结束:', url)
    return url

urls = [
   'www.1.com',
   'www.2.com',
   'www.3.com'
]

if __name__ == '__main__':
    start = time.time()
    tasks = []
    for i in urls:
        # 返回协程对象
        c = get_request(i)
        # 封装为任务对象
        task = asyncio.ensure_future(c)
        tasks.append(task)
    # 创建事件循环
    loop = asyncio.get_event_loop()
    # 将任务对象注册到事件循环且开启事件循环
    loop.run_until_complete(asyncio.wait(tasks))
    print('总耗时', time.time()-start)